package com.homework;

import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Future03 {

    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();

        System.out.println("Main Thread Start!");

        ExecutorService executorService = Executors.newSingleThreadExecutor();
        ArrayList<Future<String>> results = new ArrayList<java.util.concurrent.Future<String>>();
        for (int i = 0; i < 10; i++) {
            results.add(executorService.submit(new Task(i)));
        }
        for (Future<String> future : results) {
            try {
                System.out.println("异步处理结果为：" + future.get());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                executorService.shutdown();
            }
        }

        System.out.println("使用时间：" + (System.currentTimeMillis() - start) + " ms");

        System.out.println("Main Thread End!");
    }

}
